package com.wxmp.userwechat.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.wxmp.userwechat.domain.SysUserCpWechatEntity;
import com.wxmp.userwechat.mapper.SysUserCpWechatMapper;
import com.wxmp.userwechat.service.SysUserCpWechatService;

import me.chanjar.weixin.cp.bean.WxCpUser;

@Component("sysUserCpWechatService")
public class SysUserCpWechatServiceImpl implements SysUserCpWechatService {

	@Autowired
	private SysUserCpWechatMapper sysUserCpWechatMapper;
	
	@Override
	public SysUserCpWechatEntity getSysUserCpWechatById(Long id) {
		SysUserCpWechatEntity sysUserCpWechat = sysUserCpWechatMapper.getObjectById(id);
		return sysUserCpWechat;
	}
	
	@Override
	public List<SysUserCpWechatEntity> listSysUserCpWechatByUserIds(Long[] userids) {
		List<SysUserCpWechatEntity> listUserCpWechat = sysUserCpWechatMapper.listObjectByUserIds(userids);
		return listUserCpWechat;
	}

	@Override
	public int updateSysUserCpWechat(SysUserCpWechatEntity sysUserCpWechat) {
		return sysUserCpWechatMapper.update(sysUserCpWechat);
	}

	@Override
	public int batchInsert(List<WxCpUser> infoList) {
		int count = 0;
    	List<SysUserCpWechatEntity> userCpWechatList = new ArrayList<SysUserCpWechatEntity>();
    	//全量同步先删除之前的数据
    	sysUserCpWechatMapper.truncateSysUserCpWechat();
    	for (int i =0; i<infoList.size(); i++) {
    		WxCpUser wxcp = infoList.get(i);
    		SysUserCpWechatEntity sysUserCpWechatEntity = new SysUserCpWechatEntity(wxcp);
    		userCpWechatList.add(sysUserCpWechatEntity);
    		//分条插入，避免一次数据过大
    		if(i%100==0) {
    			count+=sysUserCpWechatMapper.batchInsert(userCpWechatList);
    			userCpWechatList = new ArrayList<SysUserCpWechatEntity>();
    		}
		}
    	count += sysUserCpWechatMapper.batchInsert(userCpWechatList);
		return count;
	}
	
	@Override
	public int deleteByWxUserId(String wxUserId) {
		int count = sysUserCpWechatMapper.deleteByWxUserId(wxUserId);
		return count;
	}
	@Override
	public List<String> listUserNotInCpWechat(String[] userIds) {
		List<String> list = new ArrayList<String>();
		list = sysUserCpWechatMapper.listUserNotInCpWechat(userIds);
		return list;
	}
	
}
